//
//  FANOAuthViewController.m
//  Weibo - OC
//
//  Created by user on 16/4/19.
//  Copyright © 2016年 user. All rights reserved.
//

#import "FANOAuthViewController.h"
#import <SVProgressHUD/SVProgressHUD.h>
#import "NetworkTools.h"
#import "FANUserAccount.h"
#import "FANUserAccountModel.h"
#import "FANWelcomeViewController.h"

@interface FANOAuthViewController ()<UIWebViewDelegate>

@property (nonatomic, strong) UIWebView *webView;

@end


static NSString * WB_APP_KEY = @"1679233831";

static NSString * WB_REDIRECT_URI = @"http://www.baidu.com/";

static NSString * WB_APP_SECRET = @"4dd3577aaf85646ef9ae5793e6da1a0e";


@implementation FANOAuthViewController

- (UIWebView *)webView{
    
    if (!_webView) {
        
        _webView = [[UIWebView alloc] init];
        
        _webView.delegate = self;
        
    }
    
    return _webView;
}

- (void)loadView{
    
    self.view = self.webView;
    
    NSString *urlString = [NSString stringWithFormat:@"https://api.weibo.com/oauth2/authorize?client_id=%@&redirect_uri=%@",WB_APP_KEY,WB_REDIRECT_URI];
    
    
    NSURL *url = [NSURL URLWithString:urlString];
    
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    
    [self.webView loadRequest:request];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
//    [self.view addSubview:self.webView];
    
    self.title = @"登录";
    
    self.navigationController.navigationBar.tintColor = [UIColor orangeColor];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"自动填充" style:UIBarButtonItemStylePlain target:self action:@selector(didAuto)];
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"关闭" style:UIBarButtonItemStylePlain target:self action:@selector(didClose)];
 
}

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{
    
    
    if (request.URL == nil) {
        
        NSLog(@"request.URL == nil");
        
        return false;
    }
    
    if ([request.URL.absoluteString hasPrefix:WB_REDIRECT_URI] && [request.URL.query containsString:@"code="]) {
        
       NSString *code = [request.URL.query substringFromIndex:5];
        
        [[FANUserAccountModel sharedAccountModel] loadaccess_token:code and:^(BOOL isSuccess) {
            
            if (isSuccess == true) {
                
                [self dismissViewControllerAnimated:false completion:^{
                    
                    [[NSNotificationCenter defaultCenter] postNotificationName:@"changeVc" object:self];
                    
//                    [UIApplication sharedApplication].keyWindow.rootViewController = [[FANWelcomeViewController alloc] init];
                    
                }];
                
//                NSLog(@"登录成功");
            }else{
                
                NSLog(@"登录失败");
            }
            
            
        }];
        
        return false;
        
    }
    
    
    return YES;
}



- (void)webViewDidStartLoad:(UIWebView *)webView{
    
    [SVProgressHUD show];
}

- (void)webViewDidFinishLoad:(UIWebView *)webView{
    
    [SVProgressHUD dismiss];
}

- (void)didClose{
    
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)didAuto{

    NSString *jsString = @"document.getElementById('userId').value='f474283547@163.com';document.getElementById('passwd').value='sgq474283547'";
    
    [self.webView stringByEvaluatingJavaScriptFromString:jsString];
}





@end
